package com.caishi.lkx.exam.api;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.caishi.lkx.common.AdminBaseApi;
import com.caishi.lkx.exam.model.PackageTagModel;
import com.caishi.lkx.exam.service.IExamService;
import com.caishi.lkx.exam.service.IPackageTagService;
import com.zzw.common.Wrappers;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@Tag(name = "套餐标签管理")
@Slf4j
@RestController
@RequestMapping("/exam/package/tag")
public class PackageTagApi extends AdminBaseApi<IPackageTagService, PackageTagModel, String> {
    @Resource
    private IPackageTagService packageTagService;
    @Resource
    private IExamService examService;

    @Override
    public IPackageTagService loadService() {
        return packageTagService;
    }

    @Override
    protected List<PackageTagModel> listCover(List<PackageTagModel> result, JSONObject otherParam) {
        return result.parallelStream().peek(r -> {
            if (StrUtil.isBlank(r.getExamId())) {
                r.setExamName("通用");
            } else {
                r.setExamName(examService.queryName(r.getExamId()));

            }
        }).toList();
    }

    @Override
    protected LambdaQueryWrapper<PackageTagModel> baseWrapper(PackageTagModel model, JSONObject otherParam) {
        return super.baseWrapper(
                        model.clearEmptyStringField("name"), otherParam)
                .like(StrUtil.isNotBlank(model.getName()), PackageTagModel::getName, model.getName());
    }

    @GetMapping("/commonAndExamTags")
    public List<PackageTagModel> getCommonAndExamTags(@RequestParam(defaultValue = "10") Integer size, @RequestParam(required = false) String examId) {
        return loadService().selectList(Wrappers.<PackageTagModel>lambdaQuery().isNull(PackageTagModel::getExamId)
                .or().eq(PackageTagModel::getExamId, "")
                .or().eq(StrUtil.isNotBlank(examId), PackageTagModel::getExamId, examId));
    }
}
